#include <algorithm>
#include <climits>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
class Solution {
  unsigned long long totlesum(vector<int> &a) {
    unsigned long long res = 0;
    for (auto p = a.begin(); p != a.end(); ++p) {
      res += *p;
    }
    return res;
  }

public:
  vector<vector<int>> restoreMatrix(vector<int> &rowSum, vector<int> &colSum) {
    unsigned long long tot = totlesum(rowSum);
    vector<vector<int>> res;
    res.resize(rowSum.size());
    for (int i = 0; i < res.size(); ++i) {
      res[i].resize(colSum.size());
    }
    for (int i = 0; i < rowSum.size(); ++i) {
      for (int j = 0; j < colSum.size(); ++j) {
        res[i][j] = 0;
      }
    }
    while (tot > 0) {
      int minrow = min_element(rowSum.begin(), rowSum.end()) - rowSum.begin();
      int mincol = min_element(colSum.begin(), colSum.end()) - colSum.begin();
      int val = min(rowSum[minrow], colSum[mincol]);
      res[minrow][mincol] = val;
      tot -= val;
      rowSum[minrow] -= val;
      if (rowSum[minrow] == 0) {
        rowSum[minrow] = INT_MAX;
      }
      colSum[mincol] -= val;
      if (colSum[mincol] == 0) {
        colSum[mincol] = INT_MAX;
      }
    }
    return res;
  }
};

int main(int argc, char *argv[]) {

  vector<int> a = {3, 8};
  vector<int> b = {4, 7};
  Solution s;
  s.restoreMatrix(a, b);
  return 0;
}
